﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Company.Database;
using Company.Database.Entities;
using Company.Database.Sqlite;
using Company.Shell.Dialogs;
using Company.Shell.Interfaces;
using HandyControl.Controls;
using System.Windows.Input;

namespace Company.Shell.ViewModels
{
    internal class UserViewModel : ObservableObject
    {
        private string message = string.Empty;
        public string Message
        {
            get => message;
            set => SetProperty(ref message, value);
        }
        private List<UserEntity>? users;
        /// <summary>
        /// 本系统所有用户
        /// </summary>
        public List<UserEntity>? Users
        {
            get => users;
            set => SetProperty(ref users, value);
        }
        
        public string Role { get; set; } = "操作员";
        public UserEntity LoginUserEntity { get; set; } = new UserEntity();
        public UserEntity NewUserEntity { get; set; } = new UserEntity();
        public ICommand? LoginCommand { get; }
        public ICommand? AddUserCommand { get; }
        public ICommand? SaveUserCommand { get; }

        public ISession Session { get; }
        private IUserRepository UserRepository { get; }
        public UserViewModel(
            IUserRepository userRepository,
            ISession session)
        {
            UserRepository = userRepository;
            Session = session;
            LoginCommand = new RelayCommand(OnLoginCommand);
            AddUserCommand = new RelayCommand(OnAddUserCommand);
            SaveUserCommand = new RelayCommand(OnSaveUserCommand);
            Users = UserRepository.GetAll();

        }

        /// <summary>
        /// 保存用户修改
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void OnSaveUserCommand()
        {
            if (Users == null) return;
            foreach (var user in Users)
            {
                UserRepository.Update(user);
            }
            Dialog.Show(new MessageDialog("保存完毕"));

        }

        /// <summary>
        /// 添加新用户
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void OnAddUserCommand()
        {
            if (string.IsNullOrEmpty(NewUserEntity.UserName))
            {
                Dialog.Show(new MessageDialog("请输入用户名"));
                return;
            }
            if (string.IsNullOrEmpty(NewUserEntity.Password))
            {
                Dialog.Show(new MessageDialog("请输入密码"));
                return;
            }
            var entity = new UserEntity();
            entity.UserName = NewUserEntity.UserName;
            entity.Password = NewUserEntity.Password;
            entity.Role = Role == "操作员" ? 0 : 1;
            var success = UserRepository.Insert(entity);
            if (success)
            {
                Dialog.Show(new MessageDialog("添加成功"));
                Users = UserRepository.GetAll();
            }
            else
            {
                Dialog.Show(new MessageDialog("添加失败"));
            }
        }

        private void OnLoginCommand()
        {
            Message = "";
            var success = UserRepository.Login(LoginUserEntity.UserName, LoginUserEntity.Password,
                out UserEntity user);
            if (success)
            {
                Session.Add(user);
                Message = "登录成功";
                LoginUserEntity.UserName = "";
                LoginUserEntity.Password = "";
            }
            else
            {
                Message = "登录失败";
            }
        }
    }
}
